//
//  LocalData.m
//  Seizure
//
//  Created by student on 2/21/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "LocalData.h"
#import "Account.h"
#import "EventLog.h"

@implementation LocalData


#pragma mark -- CONSTANTS

NSString * const LOC_KEY_NAME = @"Name";
NSString * const LOC_KEY_EMAIL = @"Email";
NSString * const LOC_KEY_APNS = @"APNSKey";
NSString * const LOC_KEY_PATIENT = @"isPatient";
NSString * const LOC_KEY_SENSITIVITY = @"Sensitivity";

NSString * const ARCHIVED_EVENTS = @"archivedEvents";


#pragma mark -- METHODS

- (void)storeAccount:(Account*)account{
    // store account to Default Prefs
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *name = account.name;
    NSString *email = account.email;
    NSString *apnsKey = account.apnsKey;
    Boolean isPatient = account.isPatient;
    NSNumber *sensitivity = account.sensitivity;
    
    [defaults setObject:name forKey:LOC_KEY_NAME];
    [defaults setObject:email forKey:LOC_KEY_EMAIL];
    [defaults setObject:apnsKey forKey:LOC_KEY_APNS];
    [defaults setBool:isPatient forKey:LOC_KEY_PATIENT];
    [defaults setObject:sensitivity forKey:LOC_KEY_SENSITIVITY];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

/**
 * Retrieves the account details last saved into default preferences.
 * If no account details are found, this means that the user logged out.
 *
 * @returns - Account details, or nil if no default preferences
 */
- (Account*)retrieveAccount{
    // get the account from where-ever
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSString *name = [defaults stringForKey:LOC_KEY_NAME];
    NSString *email = [defaults stringForKey:LOC_KEY_EMAIL];
    NSString *apnsKey = [defaults stringForKey:LOC_KEY_APNS];
    Boolean isPatient = [defaults boolForKey:LOC_KEY_PATIENT];
    NSNumber *sensitivity = [defaults objectForKey:LOC_KEY_SENSITIVITY];
    //Check that there was stuff in the default preferences
    if( name != nil && email != nil && apnsKey != nil )
    {   
        //Return a new Account with the correct login details
        return [[Account alloc] initWithName:name email:email 
                                   isPatient:isPatient apnsKey:apnsKey
                                    sensitivity:sensitivity]; 
    }
    
    //There were no preferences saved
    return nil;
}


#pragma mark -- LOCAL DATA EVENT LOGS

/**
 *  Add one event log to the list of local logs.
 */
- (void)addEventLog:(EventLog*)event
{
    NSMutableArray *currentLogs = [[NSMutableArray alloc] initWithArray:[self retrieveEventLogs]];
    //Event should be at top since it is the newest log.
    [currentLogs insertObject:event atIndex:0];
    [self storeEventLogs:currentLogs];
}

/**
 *  Stores the event logs to local data.
 */
- (void)storeEventLogs:(NSArray*)events
{
    NSData *archivedEvents = [NSKeyedArchiver archivedDataWithRootObject:events];
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:archivedEvents forKey:ARCHIVED_EVENTS];
}

/**
 *  Retrieves event logs from local data.
 */
- (NSArray*)retrieveEventLogs
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSData *archivedEvents = [defaults objectForKey:ARCHIVED_EVENTS];
    
    //If there was an object associated with the key
    if( archivedEvents != nil )
    {
        NSArray* unarchivedEvents = [NSKeyedUnarchiver unarchiveObjectWithData:archivedEvents];
        if( unarchivedEvents != nil )
            return unarchivedEvents;
    }
    
    return nil;
}

/**
 * If the User has logged out, clear all of the cached account data:
 *  account data
 *  event log data
 */
- (void)userLoggedOut
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults removeObjectForKey:LOC_KEY_NAME];
    [defaults removeObjectForKey:LOC_KEY_EMAIL];
    [defaults removeObjectForKey:LOC_KEY_APNS];
    [defaults removeObjectForKey:LOC_KEY_PATIENT];
    [defaults removeObjectForKey:LOC_KEY_SENSITIVITY];
    
    [defaults removeObjectForKey:ARCHIVED_EVENTS];
    
    [defaults synchronize];
}

@end
